package com.tarena.lbs.stock.web.rpc;

import com.tarena.lbs.pojo.stock.param.CouponStockParam;
import com.tarena.lbs.stock.api.StockApi;
import com.tarena.lbs.stock.web.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@DubboService
@Component
@Slf4j
public class StockApiImpl implements StockApi {
    @Autowired
    private StockService stockService;
    @Override
    public Boolean initCouponStock(CouponStockParam param) {
        try{
            //调用业务层 将库存初始化完成 返回布尔结果
            return stockService.initCouponStock(param);
        }catch (Exception e){
            log.error("当前优惠券:{},初始化库存数量:{},失败",e);
        }
        return false;
    }

    @Override
    public Integer getCouponNum(Integer couponId) {
        Integer num=0;
        try{
            num=stockService.getCouponNum(couponId);
        }catch (Exception e){
            log.error("查询优惠券:{},剩余库存失败",couponId,e);
        }
        return num;
    }

    @Override
    public Boolean reduceStock(Integer couponId, Integer num) {
        Boolean result=false;
        try{
            result=stockService.reduceStock(couponId,num);
        }catch (Exception e){
            log.error("扣减优惠券:{},库存数:{},失败",couponId,num,e);
        }
        return result;
    }
}
